package com.news.base;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.news.R;
import com.news.di.component.ActivityComponent;
import com.news.di.component.AppComponent;
import com.news.di.component.DaggerActivityComponent;
import com.news.di.module.ActivityModule;
import com.news.util.LogUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/3/1 0001.
 */

public abstract class BaseActivity extends AppCompatActivity {

    private Unbinder mUnBinder;
    private boolean hasProgress = false;
    protected ProgressBar mProgressBar;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getLayoutId());

        mUnBinder = ButterKnife.bind(this);

        initInject();

        initViews();
        loadData();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        setContentView(view);
    }

    @Override
    public void setContentView(View view) {
        View rootView = view;
        if (hasProgress) {
            LogUtil.d("BaseActivity", "setContentView: ");
            ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.activity_base, null);
            mProgressBar = (ProgressBar) viewGroup.findViewById(R.id.base_progress);
            viewGroup.addView(view);
            rootView = viewGroup;
        }
        super.setContentView(rootView);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnBinder.unbind();
    }

    protected ActivityComponent getActivityComponent() {
        return DaggerActivityComponent
                .builder()
                .appComponent(getAppComponent())
                .activityModule(getActivityModule())
                .build();
    }

    private ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }

    private AppComponent getAppComponent() {
        return ((BaseApp) getApplication()).getAppComponent();
    }

    public void setHasProgress(boolean hasProgress) {
        this.hasProgress = hasProgress;
    }

    public abstract int getLayoutId();

    public abstract void initViews();

    public abstract void loadData();

    protected abstract void initInject();
}
